﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using MicropleSoft.Controls.DialogBox;
using MicropleSoft.WmsApp.Library;
using MicropleSoft.WmsApp.Library.ApplicationObserver;
using MicropleSoft.WmsApp.Library.LookUpInterface;
using MicropleSoft.WmsApp.Library.ServiceFacade;

namespace MicropleSoft.Controls.QueryControls
{
    public partial class QueryControlBase : DevExpress.XtraEditors.XtraUserControl,IQueryControl
    {
        public QueryControlBase()
        {
            InitializeComponent();
            if(GlobalConfig.RunTimeMode)
            {
                ExecuteLookDataUpSourceBind(this);
            }
        }

        // ReSharper disable MemberCanBeMadeStatic.Local
        private void ExecuteLookDataUpSourceBind( Control parentControl)
        // ReSharper restore MemberCanBeMadeStatic.Local
        {
            if (parentControl is XXtraTabControl)
            {
                var tabControl = (XXtraTabControl)parentControl;
                foreach (XtraTabPage tabPage in tabControl.TabPages)
                {
                    ExecuteLookDataUpSourceBind( tabPage);
                }
            }
            else
            {
                foreach (Control control in parentControl.Controls)
                {
                    if (control is XXtraTabControl)
                    {
                        var tabControl = (XXtraTabControl)control;
                        foreach (XtraTabPage tabPage in tabControl.TabPages)
                        {
                            ExecuteLookDataUpSourceBind(tabPage);
                        }
                    }
                    else if (control is XPanelControl)
                    {
                        var xPanelControl = (XPanelControl)control;
                        foreach (Control tabPage in xPanelControl.Controls)
                        {
                            ExecuteLookDataUpSourceBind(tabPage);
                        }
                    }
                    else if (control is IDataSource)
                    {
                        //如果是数据源绑定控件，则加载数据源
                        //如果是LookUp，则绑定列，如果是GridLookUp，绑定视图
                        //另外将控件送入观察者容器中
                        //当控件窗体关闭，则从观察者容器中移除
                        //另外当缓存刷新，则将所有的观察者对象刷新一遍

                        var dataSource = (IDataSource)control;

                        if (String.IsNullOrEmpty(dataSource.DataSourceKey))
                        {
                            CustomMessageBox.Show(control.Name + "的DataSourceKey属性没有赋值！");
                            continue;
                        }

                        var baseDataWrap = (BaseDataWrap)dataSource.GetBaseDataProvider().GetBaseDataByKey(dataSource.DataSourceKey);
                        
                        if(control is ILookUpViewHeaderBuilder)
                        {
                            var lookUpViewHeaderBuilder = (ILookUpViewHeaderBuilder) control;
                            lookUpViewHeaderBuilder.BuildColumns(baseDataWrap.LookUpViewHeaderList);
                        }

                        if (dataSource.DataSourceUseCache)
                        {
                            DataSourceControlObserverFactory.CreateDataSourceControlObserver().AddControl(this,
                                                                                                          dataSource.
                                                                                                              DataSourceKey);
                        }
                    }
                }
            }
        }

        public virtual string GetCondition()
        {
            throw new NotImplementedException();
        }

        public virtual void AddCustomerCondition(string condition)
        {
            throw new NotImplementedException();
        }
    }
}
